package jp.heartdokyun.activity;

import java.util.HashMap;

import jp.heartdokyun.R;
import jp.heartdokyun.listener.GoBackListener;
import jp.heartdokyun.listener.TopBackListener;
import jp.heartdokyun.util.Utils;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class MenuActivity extends Activity {
	private ImageButton mIBOL, mIBCA, mIBNurse, mIBSailor, mIBTeacher, mIBMaid,
			mIDMinikimono, mIBBloomer;
	private SoundPool mSoundPool;
	private HashMap<Integer, Integer> soundPoolMap;
	private MediaPlayer mMediaPlayer;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		overridePendingTransition(android.R.anim.slide_in_left,
				android.R.anim.slide_out_right);
		setContentView(R.layout.menu);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		initSounds();

		mIBOL = (ImageButton) findViewById(R.id.ib_menu_ol);
		mIBOL.setOnClickListener(GameActivityClickListener);
		mIBCA = (ImageButton) findViewById(R.id.ib_menu_ca);
		mIBCA.setOnClickListener(GameActivityClickListener);
		mIBNurse = (ImageButton) findViewById(R.id.ib_menu_nurse);
		mIBNurse.setOnClickListener(GameActivityClickListener);
		mIBSailor = (ImageButton) findViewById(R.id.ib_menu_sailor);
		mIBSailor.setOnClickListener(GameActivityClickListener);
		mIBTeacher = (ImageButton) findViewById(R.id.ib_menu_teacher);
		mIBTeacher.setOnClickListener(GameActivityClickListener);
		mIBMaid = (ImageButton) findViewById(R.id.ib_menu_maid);
		mIBMaid.setOnClickListener(GameActivityClickListener);
		mIDMinikimono = (ImageButton) findViewById(R.id.ib_menu_minikimono);
		mIDMinikimono.setOnClickListener(GameActivityClickListener);
		mIBBloomer = (ImageButton) findViewById(R.id.ib_menu_bloomer);
		mIBBloomer.setOnClickListener(GameActivityClickListener);
		ImageButton ibBack = (ImageButton) findViewById(R.id.ib_menu_back);
		ibBack.setOnClickListener(new GoBackListener(this));
		
		mMediaPlayer = MediaPlayer.create(getApplicationContext(),R.raw.other_bgm);
		float volume = Utils.getBGMVolume(getApplicationContext());
		mMediaPlayer.setVolume(volume, volume);
		mMediaPlayer.setLooping(true);
		ImageButton ibo = (ImageButton) findViewById(R.id.ic_o);
		ibo.setOnClickListener(new TopBackListener(this));

		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(1500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				playSound(9, 0);
			}
		}).run();
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN
				&& event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return false;
	}

	private OnClickListener GameActivityClickListener = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			Intent itStageSelection = new Intent(MenuActivity.this,
					StageSelectionActivity.class);
			String[] arrType = getResources().getStringArray(R.array.type_name);

		
			if (arg0.getId() == R.id.ib_menu_ol) {
				itStageSelection.putExtra("CHALLENGE_TYPE",
						getString(R.string.title_ol));
				playSound(1, 0);
				itStageSelection.putExtra("CHALLENGE_TYPE_VALUE", arrType[0]);
			} else if (arg0.getId() == R.id.ib_menu_ca) {
				itStageSelection.putExtra("CHALLENGE_TYPE",
						getString(R.string.title_ca));
				itStageSelection.putExtra("CHALLENGE_TYPE_VALUE", arrType[1]);
				playSound(2, 0);
			} else if (arg0.getId() == R.id.ib_menu_nurse) {
				itStageSelection.putExtra("CHALLENGE_TYPE",
						getString(R.string.title_nurse));
				itStageSelection.putExtra("CHALLENGE_TYPE_VALUE", arrType[2]);
				playSound(3, 0);
			} else if (arg0.getId() == R.id.ib_menu_sailor) {
				itStageSelection.putExtra("CHALLENGE_TYPE",
						getString(R.string.title_sailor));
				itStageSelection.putExtra("CHALLENGE_TYPE_VALUE", arrType[3]);
				playSound(4, 0);
			} else if (arg0.getId() == R.id.ib_menu_teacher) {
				itStageSelection.putExtra("CHALLENGE_TYPE",
						getString(R.string.title_teacher));
				itStageSelection.putExtra("CHALLENGE_TYPE_VALUE", arrType[4]);
				playSound(5, 0);
			} else if (arg0.getId() == R.id.ib_menu_maid) {
				itStageSelection.putExtra("CHALLENGE_TYPE",
						getString(R.string.title_maid));
				itStageSelection.putExtra("CHALLENGE_TYPE_VALUE", arrType[5]);
				playSound(6, 0);
			} else if (arg0.getId() == R.id.ib_menu_minikimono) {
				itStageSelection.putExtra("CHALLENGE_TYPE",
						getString(R.string.title_minikimono));
				itStageSelection.putExtra("CHALLENGE_TYPE_VALUE", arrType[6]);
				playSound(7, 0);
			} else if (arg0.getId() == R.id.ib_menu_bloomer) {
				itStageSelection.putExtra("CHALLENGE_TYPE",
						getString(R.string.title_bloomer));
				itStageSelection.putExtra("CHALLENGE_TYPE_VALUE", arrType[7]);
				playSound(8, 0);
			}
			new Thread(new Runnable() {

				@Override
				public void run() {
					try {
						Thread.sleep(1500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}).run();
			startActivity(itStageSelection);
		}
	};

	@Override
	public void onDestroy() {
		mSoundPool.release();
		if (mMediaPlayer != null)
			mMediaPlayer.release();
		super.onDestroy();
	}

	@Override
	public void onResume() {
		mMediaPlayer = MediaPlayer.create(getApplicationContext(),R.raw.other_bgm);
		float volume = Utils.getBGMVolume(getApplicationContext());
		
		mMediaPlayer.setVolume(volume, volume);
		mMediaPlayer.setLooping(true);
		
		if (MainActivity.isEnableBGM) {
			mMediaPlayer.start();
		} else {
			if (mMediaPlayer.isPlaying()) {
				mMediaPlayer.pause();
				mMediaPlayer.seekTo(0);
			}
		}
		super.onResume();
	}

	@Override
	public void onPause() {
		/*if (mMediaPlayer.isPlaying()) {
			mMediaPlayer.pause();
			mMediaPlayer.seekTo(0);
			
		}
		*/
		if (mMediaPlayer != null)
			mMediaPlayer.release();
		super.onPause();
	}

	private void initSounds() {
		mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
		soundPoolMap = new HashMap<Integer, Integer>();
		soundPoolMap.put(1,
				mSoundPool.load(getApplicationContext(), R.raw.buchooo_ol, 1));
		soundPoolMap.put(2, mSoundPool.load(getApplicationContext(),
				R.raw.attentionplease_ca, 1));
		soundPoolMap.put(3, mSoundPool.load(getApplicationContext(),
				R.raw.daijoobuu_nurse, 1));
		soundPoolMap.put(4, mSoundPool.load(getApplicationContext(),
				R.raw.senpaaai_sailorsuit, 1));
		soundPoolMap.put(5, mSoundPool.load(getApplicationContext(),
				R.raw.oshietehoshiino_teacher, 1));
		soundPoolMap.put(6, mSoundPool.load(getApplicationContext(),
				R.raw.gosyujinsamaaa_maid, 1));
		soundPoolMap.put(7, mSoundPool.load(getApplicationContext(),
				R.raw.achikidearinsuka_jp, 1));
		soundPoolMap
				.put(8, mSoundPool.load(getApplicationContext(),
						R.raw.ehe_bloomers, 1));
		soundPoolMap.put(9, mSoundPool.load(getApplicationContext(),
				R.raw.kokomitoasobo, 1));

		// soundPoolMap.put(2,
		// mSoundPool.load(getApplicationContext(), R.raw.loop_33, 1));
	}

	public void playSound(int sound, int loop) {
		float volume = Utils.getVoiceVolume(getApplicationContext());
		mSoundPool.play(soundPoolMap.get(sound), volume, volume, 1, loop, 1f);
	}

}